<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery查看json格式数据插件</title>
    <link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/bootstrap-3.3.4.css">
    <link href="css/jquery.json-viewer.css" type="text/css" rel="stylesheet"/>
    <style>
        body {
            background-color: #F7F7F7
        }
    </style>
</head>
<body>
<div class="jq22-container">
    <div class="container" style="margin-top: 1em;">
        <div class="row">
            <pre id="json-renderer"></pre>
        </div>
    </div>
</div>
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="js/jquery.json-viewer.js"></script>
<script type="text/javascript">
    $(function () {
        // json数据
        var json = {
            "id": 1001,
            "type": "donut",
            "name": "Cake",
            "description": "http://www.jq22.com",
            "price": 2.55,
            "available": {
                store: 42,
                warehouse: 600
            },
            "topping": [
                {"id": 5001, "type": "None"},
                {"id": 5002, "type": "Glazed"},
                {"id": 5005, "type": "Sugar"},
                {"id": 5003, "type": "Chocolate"},
                {"id": 5004, "type": "Maple"}
            ]
        };

        //格式化json
        try {
            var input = eval('(' + JSON.stringify(json) + ')');
        } catch (error) {
            return alert("Cannot eval JSON: " + error);
        }
        var options = {
            //为Key添加双引号
            withQuotes: true
        };
        $('#json-renderer').jsonViewer(input, options);
    });
</script>
</body>
</html>